const electron = require("electron");
const animate = require("./animate");
let BrowserWindow = electron.remote ? electron.remote.BrowserWindow : electron.BrowserWindow;

const wnd = {
    animations: {
        names:{},
        
        set(name, showCallback, hideCallback) {
            this.names[name] = {
                show:showCallback,
                hide:hideCallback
            };
        },
        
        get(name) {
            return this.names[name];
        }
    },
    
    animation_support(win) {
        win.hideWithAnimation = function (animation) {
            animation = animation ? animation : 'fade';
        
            if(animation) {
                if(wnd.animations.get(animation) && wnd.animations.get(animation).hide) {
                    wnd.animations.get(animation).hide(win);
                    return;
                }
            }
        
            this.hide();
        };
    
    
        win.showWithAnimation = function (animation) {
            animation = animation ? animation : 'fade';
        
            if(animation) {
                if(wnd.animations.get(animation) && wnd.animations.get(animation).show) {
                    wnd.animations.get(animation).show(win);
                    return;
                }
            }
        
            this.show();
        };
        
        return win;
    }
};

wnd.animations.set('fade',
    function (win) {
        if(!win._second) {
            win._second = true;
            win.setOpacity(0);
        }
    
        win.show();
        
        animate({
            params: {opacity: win.getOpacity()},
            target: {opacity: 1},
            duration:200,
            easing: 'ease-out',
            update(params) {
                win.setOpacity(params.opacity);
            }
        });
    },
    
    function(win) {
        animate({
            params: {opacity: win.getOpacity()},
            target: {opacity: 0},
            duration:200,
            easing: 'ease-out',
            update(params) {
                win.setOpacity(params.opacity);
            },
            
            complete() {
                win.hide();
            }
        });
    }
);


module.exports = wnd;
